import {
  ActionType,
  ModalForm,
  PageContainer,
  ProColumns,
  ProFormDigit,
  ProFormText,
  ProFormTextArea,
  ProTable
} from "@ant-design/pro-components";
import {Button, Form} from "antd";
import React, {useRef, useState} from "react";
import {EditOutlined, PlusOutlined, SettingFilled} from "@ant-design/icons";
import {globalLoadTableApi, handleGloblApi} from "@/services/admin-api/globalApi";

type Config = {
  configId: number;
  configName: string;
  configKey: string;
  configValStr: string;
  configValInt: number;
  configRemark: string;
  delFlag: boolean;
  createTime: string;
  createUserId: number;
  createUserName: string;
  updateTime: string;
  updateUserId: number;
  updateUserName: string;
};

export default () => {

  const [modalOpen, handleModalOpen] = useState(false);
  const [form] = Form.useForm();
  const actionRef = useRef<ActionType>();
  // const [currentRow, setCurrentRow] = useState<ProductType>();

  const columns: ProColumns<Config>[] = [
    // {title: '', valueType: 'indexBorder'},
    {title: 'configId', dataIndex: 'configId', valueType: 'digit'},
    {title: 'configName', dataIndex: 'configName', valueType: 'text'},
    {title: 'configKey', dataIndex: 'configKey', valueType: 'text'},
    {title: 'configValStr', dataIndex: 'configValStr', valueType: 'text', hideInSearch: true,},
    {title: 'configValInt', dataIndex: 'configValInt', valueType: 'digit', hideInSearch: true,},
    {title: 'configRemark', dataIndex: 'configRemark', valueType: 'text', hideInSearch: true,},
    {title: '创建时间', dataIndex: 'createTime', valueType: 'dateTime', hideInSearch: true,},
    {title: '创建用户ID', dataIndex: 'createUserId', valueType: 'digit', hideInSearch: true,},
    {title: '创建用户', dataIndex: 'createUserName', valueType: 'text', hideInSearch: true,},
    {title: '更新时间', dataIndex: 'updateTime', valueType: 'dateTime', hideInSearch: true,},
    {title: '更新用户ID', dataIndex: 'updateUserId', valueType: 'digit', hideInSearch: true,},
    {title: '更新用户', dataIndex: 'updateUserName', valueType: 'text', hideInSearch: true,},
    {
      title: <SettingFilled/>,
      render: (_, record) => [
        <Button
          key={'edit_' + record.configId} size="small" icon={<EditOutlined/>} type="primary"
          onClick={async () => {
            // const copy = {...record};
            // setCurrentRow(copy);
            // setModalTitle('编辑小哥财务问题');

            form.setFieldsValue({...record});
            handleModalOpen(true);
          }}
        />,
      ],
      valueType: 'option',
    },
  ];

  return <PageContainer header={{title: '', breadcrumb: {},}}>
    <ProTable<Config>
      columns={columns} actionRef={actionRef}
      search={{span: 4}}
      headerTitle={
        <Button
          key="primary" type="primary" icon={<PlusOutlined/>} onClick={() => {
          // setModalTitle('新建小哥财务问题');
          // handleModalOpen(true);
          // setFileList([])
          // message.info("添加产品类别")
          handleModalOpen(true)
        }}/>
      }
      request={async (param) => globalLoadTableApi('/sys/configTable', param)}
      rowKey={(record) => record.configId}
      columnsState={{
        defaultValue: {
          delFlag: {show: false},
          createUserId: {show: false},
          updateUserId: {show: false}
        }
      }}/>

    <ModalForm
      modalProps={{destroyOnClose: true}} autoFocusFirstInput submitTimeout={15000}
      width="400px" open={modalOpen} onOpenChange={handleModalOpen} form={form}
      onFinish={async (value) => {
        console.log('value = ', value);
        let success = await handleGloblApi('/sys/configSaveOrUpdate', value)

        if (success) {
          actionRef.current?.reload();
          return true
        }

      }}
    >
      <ProFormDigit label="configId" name="configId" hidden/>
      <ProFormText label="configName" rules={[{required: true}]} name="configName"/>
      <ProFormText label="configKey" rules={[{required: true}]} name="configKey"/>
      <ProFormText label="configValStr" name="configValStr"/>
      <ProFormDigit label="configValInt" name="configValInt"/>
      <ProFormTextArea label="configRemark" rules={[{required: true}]} name="configRemark"/>
    </ModalForm>

  </PageContainer>
}
